<template>
    <div id="LoginWrap">
        <header>
            <div class="container">
                <h1><a href="javascript:;">小兔鲜</a></h1>
                <h3>欢迎登陆</h3>
                <router-link to="/"> 进入网站首页 </router-link>
            </div>
        </header>
        <section>
            <div class="loginwrap">
                <nav>
                    <a href="javascript:;"> 账户登录</a>
                    <a href="javascript:;"> 扫码登陆</a>
                </nav>
                <div class="account">
                    <div class="toggle">
                        <a href="">使用短信登录</a>
                    </div>
                    <form>
                        <div class="form_item">
                            <div class="inp">
                                <i></i>
                                <input type="text" placeholder="请输入用户名" :class="{ red_border: red, blue_border: blue }"
                                    @focus="blue = true, red = false" @blur="blue = false, UnameBorder()"
                                    v-model="Uname">
                            </div>
                            <div class="error"><span v-if="span1">请输入用户名</span><span v-if="span2">字母开头且6-20个字符</span>
                            </div>
                        </div>
                        <div class="form_item">
                            <div class="inp">
                                <i></i>
                                <input type="password" placeholder="请输入密码"
                                    :class="{ red_border: red2, blue_border: blue2 }"
                                    @focus="blue2 = true, red2 = false" @blur="blue2 = false, PasswordBorder()"
                                    v-model="Password">
                            </div>
                            <div class="error"><span v-if="span3">请输入密码</span><span v-if="span4">密码6-24个字符</span></div>
                        </div>
                        <div class="form_item">
                            <div class="agree">
                                <input type="checkbox" v-model="checked">
                                <span>我已同意</span>
                                <a href="javascript:;">《隐私条款》</a>
                                <span>和</span>
                                <a href="javascript:;">《服务条款》</a>
                            </div>
                            <div class="error" v-if="!checked"> 请勾选登录协议</div>
                        </div>
                        <a href="javascript:;" class="btn" @click="Login">登录</a>
                    </form>
                    <div class="action">
                        <a href="">
                            <img src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png" alt="">
                        </a>
                        <div>
                            <a href="javascript:;">忘记密码</a>
                            <a href="javascript:;">免费注册</a>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>
</template>

<script>
export default {
    data() {
        return {
            red: false,
            blue: false,
            Uname: '',
            span1: false,
            span2: false,
            Password: '',
            red2: false,
            blue2: false,
            span3: false,
            span4: false,
            checked: true
        }
    },
    methods: {
        UnameBorder() {
            if (this.Uname.length == '') {
                this.red = true
                this.span1 = true
                this.span2 = false
            }
            if (this.Uname.length < 6 && this.Uname.length > 0 || this.Uname[0] % 1 == 0) {  // 小于6位且第一位是数字
                this.red = true
                this.span2 = true
                this.span1 = false
            }
            if (this.Uname.length >= 6 && this.Uname[0] % 1 != 0) {
                // this.red = false
                this.span2 = false
                this.span1 = false
            }
        },
        PasswordBorder() {
            if (this.Password == '') {
                this.red2 = true
                this.span3 = true
                this.span4 = false
            }
            if (this.Password.length < 6 && this.Password.length > 0) {
                this.red2 = true
                this.span3 = false
                this.span4 = true
            }
            if (this.Password.length >= 6) {
                // this.red2 = false
                this.span4 = false
                this.span3 = false
            }
        },
        Login() {
            // this.$store.commit('setUname', this.Uname)
            if (!this.span1 && !this.span2 && !this.span3 && !this.span4) {
                this.axios.post('https://apipc-xiaotuxian-front.itheima.net/login', { account: this.Uname, password: this.Password }).then(res => {
                    console.log(res);
                    if (res.data.msg == '操作成功') {
                        console.log(res.data);
                        this.$store.commit('setuserInfor', this.Uname)
                        this.$store.commit('user',res.data.result)
                        this.$router.push('/')
                    }
                }).catch(res => {
                    alert(res.response.data.message);
                })
            }

        }
    },
    mounted() {

    }
}
</script>
<style lang="scss" scoped>
#LoginWrap {
    header {
        background: rgb(255, 255, 255);
        border-bottom: 1px solid rgb(228, 228, 228);

        .container {
            display: flex;
            align-items: center;
            justify-content: space-between;

            h1 {
                width: 200px;

                a {
                    display: block;
                    height: 132px;
                    width: 100%;
                    text-indent: -9999px;
                    background: url('http://erabbit.itheima.net/img/logo.0940ebb5.png') center 18px / contain no-repeat;
                }
            }

            h3 {
                flex: 1 1 0%;
                font-size: 24px;
                font-weight: 400;
                // margin-bottom: 38px;
                margin-left: 20px;
                color: rgb(102, 102, 102);
                text-align: left;
            }

            a {
                width: 120px;
                // margin-bottom: 38px;
                font-size: 16px;
            }
        }
    }

    section {
        background: url(http://erabbit.itheima.net/img/login-bg.696efec3.png) 50% center / cover no-repeat;
        height: 488px;
        position: relative;

        .loginwrap {
            width: 380px;
            background: #fff;
            min-height: 400px;
            position: absolute;
            left: 50%;
            top: 54px;
            transform: translate3d(100px, 0, 0);
            box-shadow: 0 0 10px rgb(0 0 0 / 15%);

            nav {
                height: 55px;
                border-bottom: 1px solid rgb(245, 245, 245);
                display: flex;
                padding: 0px 40px;
                text-align: center;
                align-items: center;


                a {
                    flex: 1;
                    line-height: 1;
                    display: inline-block;
                    font-size: 18px;
                    position: relative;
                }

                .active {
                    color: rgb(39, 186, 155);
                    font-weight: 700;
                }
            }

            .account {
                .toggle {
                    padding: 15px 40px;
                    text-align: right;

                    a {
                        color: #27ba9b;
                    }
                }

                form {
                    padding: 0 40px;

                    .form_item {
                        margin-bottom: 28px;

                        .inp {
                            position: relative;
                            height: 36px;

                            i {
                                width: 34px;
                                height: 34px;
                                background: #cfcdcd;
                                color: #fff;
                                position: absolute;
                                left: 1px;
                                top: 1px;
                                text-align: center;
                                line-height: 34px;
                                font-size: 18px;
                            }

                            input {
                                padding-left: 44px;
                                border: 1px solid #cfcdcd;
                                height: 36px;
                                line-height: 36px;
                                width: 100%;
                            }

                            .blue_border {
                                border-color: #27ba9b;
                            }

                            .red_border {
                                border-color: #cf4444;
                            }
                        }

                        .error {
                            position: absolute;
                            font-size: 12px;
                            line-height: 28px;
                            color: #cf4444;
                        }

                        .agree {
                            a {
                                color: #069;
                            }

                            span {
                                &:nth-of-type(1) {
                                    margin-left: 5px;
                                }
                            }
                        }

                    }

                    .btn {
                        display: block;
                        width: 100%;
                        height: 40px;
                        color: #fff;
                        text-align: center;
                        line-height: 40px;
                        background: #27ba9b;
                    }
                }

                .action {
                    padding: 20px 40px;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;

                    div {
                        a {
                            color: #999;
                            margin-left: 10px;
                        }
                    }
                }
            }
        }
    }
}
</style>